package DEEPERsource.DEEPERsource.source.parser;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.ling.TaggedWord;

/**
 * Word count starts from 0.
 * 
 * Represents enriched sentence structure.
 * Main reason why I need this class is because I want to have 
 * direct mappings from nodes in dependency and parse trees to
 * words in the sentence. It is not always obvious, since different 
 * parsers (and probably different settings of one parser) could 
 * provide different word indexing (e.g. with/without punctuation or
 * splitting hyphenated words).
 * 
 * Moreover, other information about specific words (such as 
 * interactions, that the word takes part in) could be accumulated 
 * here. WRONG! IT SHOULDN"T BE HERE.
 * 
 *  I don't want to provide an abstract implementation for different 
 *  parsers because the class is quite simple. To add support for a
 *  specific parser one should add constructor that takes the specific 
 *  parser output as argument. 
 * @author tfayruzo
 *
 */
public class StructuredSentence implements Serializable {
	
	List<Word> sentence;
	
	/**
	 * Constructor for StanfordParser implementation.
	 * @param sentence
	 */
	public StructuredSentence(Sentence<TaggedWord> sentence){
		this.sentence = new ArrayList<Word>();
		for(TaggedWord word : sentence){
			Word w = new Word();
			w.key = word.word();
			w.features.put("tag", word.tag());
			this.sentence.add(w);
		}		
	}
	
	public String getWord(int i){
		if(i>1000){
			i -= 1000;
		}
		return sentence.get(i).key;
	}
	
	public String getTag(int i){
		if(i>1000){
			i -= 1000;
		}
		return (String)sentence.get(i).features.get("tag");
	}
	
	/**
	 * Returns the index of the word in the sentence.
	 * If the word occurs more than once, returns the 
	 * index of the first occurence.
	 * @param word
	 * @return
	 */
	public int getIndex(String word){
		int i = 0;
		for(Word w : sentence){
			if(w.key.contains(word)){
				return i;
			}
			i++;
		}
		return -1;
	}

	class Word implements Serializable{
		String key;
		Map<String,Object> features = new HashMap<String,Object>();
	}
}
